Declare Function FindWindow Lib "User" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Integer
Declare Function GetMenu Lib "User" (ByVal hWnd As Integer) As Integer
Declare Function InsertMenu Lib "User" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer, ByVal wIDNewItem As Integer, ByVal lpNewItem As Any) As Integer
Declare Sub DrawMenuBar Lib "User" (ByVal hWnd As Integer)
Declare Function CreatePopupMenu Lib "User" () As Integer
Declare Function AppendMenu Lib "User" (ByVal hMenu As Integer, ByVal wFlags As Integer, ByVal wIDNewItem As Integer, ByVal lpNewItem As Any) As Integer
Const MF_BYCOMMAND = &H0
Const MF_BYPOSITION = &H400
Const MF_INSERT = &H0
Const MF_POPUP = &H10
Const MF_STRING = &H0
Const MF_ENABLED = &H0
Const WM_COMMAND = &H111
Const WM_MENUSELECT = &H11F
Dim wParam As Integer
Sub Command1_Click ()
'Get Notepad's window handle
Handle% = FindWindow("notepad", 0&)
'Determine if Notepad is running or not
If Handle% = 0 Then
Msg$ = "Notepad is not running. Should I start it?"
'Prompt to start Notepad
Ans% = MsgBox(Msg$, 36)
If Ans% = 6 Then
'Start Notepad
X% = Shell("notepad")
If X% Then
'Now get its handle again
Handle% = FindWindow("notepad", 0&)
Else
'Something's wrong with Notepad
Msg$ = "Can't start Notepad."
MsgBox Msg$
Exit Sub
End If
Else
Exit Sub
End If
End If
'Get the menu handle for Notepad
hMenu% = GetMenu(Handle%)
'Create a popup menu
hMenuPopup% = CreatePopupMenu()
X% = AppendMenu(hMenuPopup%, MF_ENABLED Or MF_STRING, 50, "Item &1")
X% = AppendMenu(hMenuPopup%, MF_ENABLED Or MF_STRING, 51, "Item &2")
X% = AppendMenu(hMenuPopup%, MF_ENABLED Or MF_STRING, 52, "Item &3")
X% = AppendMenu(hMenuPopup%, MF_ENABLED Or MF_STRING, 53, "Item &4")
'Add a top level menu item to Notepad's menu
Success% = AppendMenu(hMenu%, MF_STRING Or MF_POPUP, hMenuPopup%, "&Programs") 'Adds menu
'Redraw the menu
DrawMenuBar (Handle%)
End Sub
Sub Form_Unload (Cancel As Integer)
End
End Sub
Sub mnuExit_Click ()
Unload Me
End Sub
Sub MsgBlaster1_Message (MsgVal As Integer, wParam As Integer, lParam As Long, ReturnVal As Long)
If MsgVal = WM_MENUSELECT Then
Select Case wParam
Case 0
MsgBox "Item 1 was selected"
Case 1
MsgBox "Item 2 was selected"
Case 2
MsgBox "Item 3 was selected"
Case 3
MsgBox "Item 4 was selected"
End Select
End If
End Sub
Sub SubClass1_AfterDefProcess (wnd As Integer, Msg As Integer)
Select Case wParam
Case 0
MsgBox "Item 1 was selected"
Case 1
MsgBox "Item 1 was selected"
Case 2
MsgBox "Item 1 was selected"
Case 3
MsgBox "Item 1 was selected"
End Select
End Sub
Sub SubClass1_MsgQueue (wnd As Integer, Msg As Integer, wp As Integer, lp As Long, retval As Long, dodef As Integer)